Passed
Pull Request — master (#90)
by Alejandro
03:21
created

paths.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
dl 0
loc 1
rs 10
c 0
b 0
f 0
nop 1
1
2
const path = require('path');
3
const fs = require('fs');
4
const url = require('url');
5
6
// Make sure any symlinks in the project folder are resolved:
7
// https://github.com/facebook/create-react-app/issues/637
8
const appDirectory = fs.realpathSync(process.cwd());
9
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
10
11
const envPublicUrl = process.env.PUBLIC_URL;
12
13
function ensureSlash(inputPath, needsSlash) {
14
  const hasSlash = inputPath.endsWith('/');
15
16
  if (hasSlash && !needsSlash) {
17
    return inputPath.substr(0, inputPath.length - 1);
18
  } else if (!hasSlash && needsSlash) {
19
    return `${inputPath}/`;
20
  }
21
22
  return inputPath;
23
}
24
25
const getPublicUrl = (appPackageJson) =>
26
  envPublicUrl || require(appPackageJson).homepage;
27
28
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
29
// "public path" at which the app is served.
30
// Webpack needs to know it to put the right <script> hrefs into HTML even in
31
// single-page apps that may serve index.html for nested URLs like /todos/42.
32
// We can't use a relative path in HTML because we don't want to load something
33
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
34
function getServedPath(appPackageJson) {
35
  const publicUrl = getPublicUrl(appPackageJson);
36
  const servedUrl =
37
    envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
38
39
  return ensureSlash(servedUrl, true);
40
}
41
42
const moduleFileExtensions = [
43
  'web.mjs',
44
  'mjs',
45
  'web.js',
46
  'js',
47
  'web.ts',
48
  'ts',
49
  'web.tsx',
50
  'tsx',
51
  'json',
52
  'web.jsx',
53
  'jsx',
54
];
55
56
// Resolve file paths in the same order as webpack
57
const resolveModule = (resolveFn, filePath) => {
58
  const extension = moduleFileExtensions.find((extension) =>
59
    fs.existsSync(resolveFn(`${filePath}.${extension}`)));
60
61
  if (extension) {
62
    return resolveFn(`${filePath}.${extension}`);
63
  }
64
65
  return resolveFn(`${filePath}.js`);
66
};
67
68
// config after eject: we're in ./config/
69
module.exports = {
70
  dotenv: resolveApp('.env'),
71
  appPath: resolveApp('.'),
72
  appBuild: resolveApp('build'),
73
  appPublic: resolveApp('public'),
74
  appHtml: resolveApp('public/index.html'),
75
  appIndexJs: resolveModule(resolveApp, 'src/index'),
76
  appPackageJson: resolveApp('package.json'),
77
  appSrc: resolveApp('src'),
78
  appTsConfig: resolveApp('tsconfig.json'),
79
  yarnLockFile: resolveApp('yarn.lock'),
80
  testsSetup: resolveModule(resolveApp, 'src/setupTests'),
81
  proxySetup: resolveApp('src/setupProxy.js'),
82
  appNodeModules: resolveApp('node_modules'),
83
  publicUrl: getPublicUrl(resolveApp('package.json')),
84
  servedPath: getServedPath(resolveApp('package.json')),
85
};
86
87
module.exports.moduleFileExtensions = moduleFileExtensions;
88